
const {router} = require('../../lib/system')

router.get('/', async (ctx) => {
    ctx.body = "app/后台管理/用户管理"
})
// 文章列表
router.post('/list', async (ctx) => {
    let q = ctx.request.body.params;
    let page = q.page-1;
    let size = q.size;
    // 总条数
    let total;
    // 查询到的数据
    let data;
    // 查询语句
    let sql = 'SELECT * FROM article_list WHERE is_delete=1 ORDER BY add_time DESC limit ?,? ';
    let total_sql = 'SELECT count(*) as total FROM article_list WHERE is_delete=1 ';
    await ctx.pool.query(sql,[page*size,size]).then(res=>{
        data = res
    })
    await ctx.pool.query(total_sql).then(res=>{
        total = res[0].total
    })
    ctx.success({total,data})
})
// 添加新闻
router.get('/add', async (ctx) => {
    let date = new Date().getTime();
    let sql = `INSERT INTO article_list(title,author,description,add_time) VALUES('标题','作者','描述',${new Date().getTime()})`;
    await ctx.pool.query(sql).then(res=>{
        ctx.success(res)
    });

})
router.get('/query/:id', async (ctx) => {
    let d = ctx.params;
    ctx.body = "文章查询" + d.id
})
// 修改新闻
router.get('/edit', async (ctx) => {
    let d;
    await ctx.pool.add({
        title:'article_list',
        value:{
            title:'标题',
            author:'作者',
            description:'文章描述',
            add_time:new Date().getTime()
        }
    }).then(res=>{
        d = res
    })
    ctx.body = "文章编辑" + Object.toString(d)
})
// 删除新闻
router.post('/delete', async (ctx) => {
    let id = ctx.request.body.params.id;
    console.log('delete:',id);
    if(id == 0 || id == ''){
        ctx.fail('没ID啊')
    }
    let sql = 'DELETE FROM article_list WHERE id=?' 
    await ctx.pool.query(sql,[id]).then((res)=>{
        ctx.success('删除成功')
    }).catch((err)=>{ctx.fail(err)})
})

module.exports = router;